import { PageContainer } from '@ant-design/pro-layout';
import { Table,Card,Button,Modal, Input, message } from 'antd';
import {SearchOutlined,PlusOutlined} from '@ant-design/icons'
import {UseSearch,UseTable} from '../../../components'
import React from 'react';

import moment from 'moment';
import InnovationProjectController from '@/services/InnovationProject/InnovationProjectController';
import { getStr } from '@/components/function';
import Detail from '../detail'
import OfferedController from '@/services/OfferedProject/OfferedController';

const {confirm} = Modal
class CanApply extends React.Component {
    constructor(props) {
        super(props);
        this.state = { 
            dataSource:[],
            freshen:false,
            userInfo:JSON.parse(sessionStorage.getItem("userInfo"))
         };
    }
    showNewProject=()=>{
        this.setState({
            visibleNewProject:true
        })
    }
    freshen=()=>{
        this.setState({freshen:!this.state.freshen})
    }
    handleSearch=(query)=>{
        this.setState({query})
        this.freshen()

    }
    reset=()=>{
        this.setState({query:{}})
        this.freshen()

    }
    getList=(query)=>{
        query.userName = this.state.userInfo.userName
        return OfferedController.getCanApplyList(query)
    }
    onCancel=()=>{
        this.setState({visibleNewProject:false,visibleResult:false})
        this.freshen()
    }
    toDetail=(rowData)=>{
        this.setState({
            rowData,
            visibleDetail:true
        })
    }
    toResult=(rowData)=>{
        this.setState({
            rowData,
            visibleResult:true
        })
    }
    onRef=(ref)=>{
        this.setState({
            modal:ref
        })
    }
    toStop=(rowData)=>{
        let that = this
        confirm({
            title:'提示',
            content:'确定要终止该任务吗？',
            onOk(){
                that.freshen()
            }
        })
    }
    toFiles=(text)=>{
        this.setState({
            visibleFile:true,
            dataSource:JSON.parse(text)
        })
    }
    toApply=(rowData)=>{
        let {userInfo} = this.state
        let that = this
        let params={
            projectCode:rowData.projectCode,
            projectName:rowData.projectName,
            name:userInfo.name,
            userName:userInfo.userName,
        }
        console.log(params)
        confirm({
            title:"提示",
            content:`您确定要领取《${rowData.projectName}》项目吗？`,
            onOk(){
                OfferedController.applyOffered(params).then(r=>{
                    if(r&&r.data===1){
                        message.success('领取成功')
                        that.freshen()
                    }
                })
            }
        })
    }
    render() {
        const {rowData,visibleFile,freshen,visibleDetail,query,dataSource}=this.state
        const columns=[

            {title:'项目名称',dataIndex:'projectName',align:'center'},
            {title:'所属部门',dataIndex:'department',align:'center',render:text=>text&&text.split("/")[1]},
            {title:'任务描述',dataIndex:'description',align:'center',render:text=>getStr(text)},
            {title:'发布人',dataIndex:'createdBy',align:'center'},
            {title:'发布时间',dataIndex:'createdAt',align:'center'},
            {title:'相关附件',dataIndex:'fileItems',align:'center',render:text=>text&&<a onClick={this.toFiles.bind(this,text)}>查看</a>},
            {title:'计划认领人数',dataIndex:'participantNumber',align:'center'},
            {title:'级别',dataIndex:'level',align:'center'},
            {title:'审核结果',dataIndex:'applyReviewResult',align:'center'},
            {title:'操作',align:'center',render:(text,rowData)=><div>
                <Button onClick={this.toApply.bind(this,rowData)} type={'primary'} style={{margin:5}}>领取</Button>
                </div>},
        ]
        const fileColumns=[
            {title:'序号',dataIndex:'url',render:(text,row,index)=>index+1},
            {title:'文件名',dataIndex:'fileName'},
            {title:'描述',dataIndex:'description',render:text=>getStr(text)},
            {title:'操作',dataIndex:'url',render:(text,row,index)=><a href={text} download={row.fileName}>下载</a>},
        ]
        const searchItem=[
            {name:'负责人姓名',keyName:'leaderUserName',valueType:'text'},
            {name:'项目名称',keyName:'projectName',valueType:'text'},
        ]
        return (
            <div>
                <Card>
                <UseSearch searchItem={searchItem} handleSearch={this.handleSearch} reset={this.reset}/>  
                </Card>
                <Card>
                    <UseTable columns={columns} getData={this.getList} bordered rowKey={'projectCode'} refresh={freshen} query={query}/>
                </Card>
             
                <Modal
                visible={visibleDetail}
                title={'项目详情'}
                onCancel={()=>this.setState({visibleDetail:false})}
                footer={false}
                width={"80%"}
                >
                    {visibleDetail&&<Detail rowData={rowData}/>}
                </Modal>
                {visibleFile&&<Modal
                visible={visibleFile}
                onCancel={()=>this.setState({visibleFile:false})}
                footer={false}
                >
                    <Table dataSource={dataSource} columns={fileColumns} bordered rowKey={'url'} />
                </Modal>}
                </div>
        );
    }
}

export default CanApply;